import {
	CmdTypeEnum,
	CustomUiCmd,
	ExtPackageJsonExtra,
	HeadlessCmd,
	TemplateUiCmd
} from "@kksh/api/models"
import type { CommandLaunchers, OnExtCmdSelect } from "@kksh/ui/types"
import * as v from "valibot"
import { onCustomUiCmdSelect, onHeadlessCmdSelect, onTemplateUiCmdSelect } from "./ext"
import { onQuickLinkSelect } from "./quick-links"

const onExtCmdSelect: OnExtCmdSelect = (
	ext: ExtPackageJsonExtra,
	cmd: CustomUiCmd | TemplateUiCmd | HeadlessCmd,
	{ isDev, hmr }: { isDev: boolean; hmr: boolean }
) => {
	switch (cmd.type) {
		case CmdTypeEnum.UiIframe:
			onCustomUiCmdSelect(ext, v.parse(CustomUiCmd, cmd), { isDev, hmr })
			break
		case CmdTypeEnum.UiWorker:
			onTemplateUiCmdSelect(ext, v.parse(TemplateUiCmd, cmd), { isDev, hmr })
			break
		case CmdTypeEnum.HeadlessWorker:
			onHeadlessCmdSelect(ext, v.parse(HeadlessCmd, cmd), { isDev, hmr })
			break
		default:
			console.error("Unknown command type", cmd.type)
	}
}

export const commandLaunchers = { onExtCmdSelect, onQuickLinkSelect } satisfies CommandLaunchers
